package com.singularity.common.service.impl;

import com.singularity.common.mapper.UserMapper;
import com.singularity.common.domain.dto.user.UserListDTO;
import com.singularity.common.domain.param.user.UserListParam;
import com.singularity.common.service.RoleService;
import com.singularity.common.service.UserService;
import com.singularity.utils.page.PageUtil;
import com.singularity.utils.page.ResponseDTOWithPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 天
 *
 * @description: 用户接口实现
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final RoleService roleService;

    @Autowired
    public UserServiceImpl(UserMapper userMapper, RoleService roleService) {
        this.userMapper = userMapper;
        this.roleService = roleService;
    }

    @Override
    public ResponseDTOWithPage<UserListDTO> getUserList(UserListParam userListParam) {
        PageUtil.startPage(userListParam,true,UserListDTO.class);
        List<UserListDTO> list = userMapper.getUserList(userListParam);
        List<UserListDTO> finalList = list.stream()
                .peek(userListDTO -> userListDTO.setRoles(roleService.getRoleInfo(userListDTO.getId())))
                .collect(Collectors.toList());
        return PageUtil.getListDTO(finalList, userListParam);
    }
}
